<?php

class RecordController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
         $this->_helper->layout->setLayout('layout-expediente');

    }

    public function indexAction()
    {
        // action body
      try{
            // action body
               //$fechaInicial = date("Y-m-d H:i:s ");
            //$fechaFinal = date("Y-m-d H:i:s ");
          if ($this->getRequest()->isPost()) 
          {
            $placa = $_POST['placa'];
            $cliente = $_POST['nombre'];
            if(!empty($placa)){

            } else{
              
              if(!empty($cliente)){

              } else {
                if(!empty($cliente)){

                }                
              } 
            }
            

            $fechaInicial = $_POST['date_start'];
            $fechaFinal = $_POST['date_end'];

            $start_date_formatted = date('Y-m-d', strtotime($fechaInicial)).' 00:00:00';          
            $end_date_formatted = date('Y-m-d', strtotime($fechaFinal)).' 00:00:00';

            $customers = new Application_Model_DbTable_Record();
            $this->view->expedientes = $customers->findRecord($placa,$fechaInicial,$fechaFinal);
          }

       } 
        catch(Exception $e){
         //   $logger = Zend_Registry::get('logger');     
           // $logger->log($e->getMessage()."; File Name: ".$e->getFile ( )."; Line: ".$e->getLine ( ), Zend_Log::ERR);
            throw $e;
        }
    }

    public function findAction()
    {
       $this->_helper->layout->setLayout('layout-home');
        $customers = new Application_Model_DbTable_Record();
             $this->view->expedientes = $customers->findRecordProforma();

    }

    public function addAction()
    {
        // action body
        $fecha = date("Y-m-d"); 
        echo $fecha;
        if ($this->getRequest()->isPost()) 
        {
        	echo "Pruebaif";
            if(!empty($_POST['placa'])){
            	$placa = $_POST['placa'];
            	$type = $_POST['type'];
                $observation = $_POST['observacion'];
                $count = $_POST['count'];
                $pieceDescription = $_POST['piecedescription'];
                $piecePrice = $_POST['pieceprice']; 
                $descriptionService = $_POST['piecedescriptionService'];
                $priceService = $_POST['piecepriceService']; 
               
               $customers = new Application_Model_DbTable_DetailRecord();
               foreach(array_keys($count) as $i) { 
                	# code...
               		$dato1 = $count[$i];
               		$dato2 = $pieceDescription[$i];
               		$dato3 = $piecePrice[$i];

               		
                    $customers->addDetailRecord($placa, $fecha,$dato1, $dato2, $dato3,'R');//R = Respuesto
                }


                foreach(array_keys($descriptionService) as $i) { 
                	# code...
               		
               		$dato2 = $descriptionService[$i];
               		$dato3 = $priceService[$i];

               		
                    $customers->addDetailRecord($placa, $fecha, '' , $dato2, $dato3,'S');//S = Servicio
                }



                    $records = new Application_Model_DbTable_Record();
                    $records->addRecord($placa, $fecha,$observation,$type);//P = Proforma

              }  


        }

    }

    public function findDetailRecordAction()
    {
        // action body
      
    }

    public function editAction()
    {
        // action body
        $placa = $this->_getParam('placa', 0);
        //Viene de la pantalla de home
        if ($placa != null) {
            try{
                $recordDB = new Application_Model_DbTable_Record();  
                $DetailRecordDB = new Application_Model_DbTable_DetailRecord();   

               /* $this->view->expedientes = $recordDB->getRecord($placa);                         
                /*$this->view->records = $DetailRecordDB->getDetailRecord($placa);
                $this->view->placa = $placa;*/
            }
            catch(Exception $e){
                throw $e;
            }      
        }
/*
        //Verifica si es necesario actualizar 
        if(!empty($_POST['type'])){

            $count = $_POST['count'];
            $pieceDescription = $_POST['piecedescription'];
            $piecePrice = $_POST['pieceprice']; 
            $descriptionService = $_POST['piecedescriptionService'];
            $priceService = $_POST['piecepriceService']; 
            
            //Se elimina los registros


            //Se insertan los registros nuevos
            $TotalRepuesto = 0;
            $customers = new Application_Model_DbTable_DetailRecord();
            foreach(array_keys($count) as $i) { 
              # code...
              $countRepuestos = $count[$i];
              $description = $pieceDescription[$i];
              $price = $piecePrice[$i];
              $Total = $countRepuestos * $price ;

              $TotalRepuesto += $Total;
              $customers->addDetailRecord($placa, $fecha,$countRepuestos, $description, $price,'R');//R = Respuesto
            }

              
            $totalService = 0;
            foreach(array_keys($descriptionService) as $i) { 
              # code...
              
              $description = $descriptionService[$i];
              $price = $priceService[$i];
              
              $totalService += $price;
              $customers->addDetailRecord($placa, $fecha, 1 , $description, $price,'S');//S = Servicio
            }
            
            //Se el usuario decidio guardar el expediente y dejar de ser proforma 
            if($_POST['type'] == "R"){
              
            }
        }*/
    }


}



